package com.lb.jwitter.action.report;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMDiemDat;
import com.lb.jwitter.service.TblQLDTTXADMDiemDatService;
import com.lb.jwitter.service.TblQLDTTXADMKetQuaXetTuyen;
import com.lb.jwitter.service.TblQLDTTXADMKetQuaXetTuyenService;
import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;
import com.lb.jwitter.service.TblQLDTTXADMThangDiem;
import com.lb.jwitter.service.TblQLDTTXADMThangDiemService;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemTong;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemTongService;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoDiemXetTuyenService;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyenService;
import com.lb.jwitter.service.TblQLDTTXAThongBaoTuyenSinh;
import com.lb.jwitter.service.TblQLDTTXAThongBaoTuyenSinhService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.ViewHoSoXetTuyenDiem;
import com.lb.jwitter.service.ViewHoSoXetTuyenDiemService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblQLDTTXAXetTuyenReportAction {
	private Message msg; 
    public Message getMsg() { return msg; }



    private TblQLDTTXAHoSoDiemXetTuyen tblQLDTTXAHoSoDiemXetTuyen; 
    public TblQLDTTXAHoSoDiemXetTuyen getTblQLDTTXAHoSoDiemXetTuyen () {
        return this.tblQLDTTXAHoSoDiemXetTuyen ;
 }

    private Collection<TblQLDTTXAHoSoDiemXetTuyen> tblQLDTTXAHoSoDiemXetTuyens; 
    public Collection<TblQLDTTXAHoSoDiemXetTuyen> getTblQLDTTXAHoSoDiemXetTuyens() {
        return this.tblQLDTTXAHoSoDiemXetTuyens; 
      } 

    private TblQLDTTXAHoSoDiemXetTuyenService tblQLDTTXAHoSoDiemXetTuyenService; 

    private Collection<TblQLDTTXAHoSoXetTuyen> tblQLDTTXAHoSoXetTuyens; 
    public Collection<TblQLDTTXAHoSoXetTuyen> getTblQLDTTXAHoSoXetTuyens() {
         return this.tblQLDTTXAHoSoXetTuyens; 
    }

    private TblQLDTTXAHoSoXetTuyenService tblQLDTTXAHoSoXetTuyenService; 

    private Collection<TblQLDTTXADMThangDiem> tblQLDTTXADMThangDiems; 
    public Collection<TblQLDTTXADMThangDiem> getTblQLDTTXADMThangDiems() {
         return this.tblQLDTTXADMThangDiems; 
    }

    private TblQLDTTXADMThangDiemService tblQLDTTXADMThangDiemService; 

	
	  private TblTaiKhoan taiKhoanDangNhap; 
	 public TblTaiKhoan getTaiKhoanDangNhap() { 
	     return taiKhoanDangNhap; 
	 } 
	 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	     this.taiKhoanDangNhap = taiKhoanDangNhap; 
	 } 
	 private TblQuyenChiTietService tblQuyenChiTietService;
		 @Autowired // Injected by Spring 
	     ApplicationContextProvider contextProvider; 
	
	 private Collection<ViewHoSoXetTuyenDiem> viewHoSoXetTuyenDiems;
	 private ViewHoSoXetTuyenDiemService viewHoSoXetTuyenDiemService;
	 
	 private List<TblQLDTTXAThongBaoTuyenSinh> tblQLDTTXAThongBaoTuyenSinhs;
	 private TblQLDTTXAThongBaoTuyenSinhService tblQLDTTXAThongBaoTuyenSinhService;	
	 
	 private Collection<TblQLDTTXADMKetQuaXetTuyen> tblQLDTTXADMKetQuaXetTuyens;
	 private TblQLDTTXADMKetQuaXetTuyenService tblQLDTTXADMKetQuaXetTuyenService;
	 
	 private Collection<TblQLDTTXAHoSoDiemTong> tblQLDTTXAHoSoDiemTongs;
	 private TblQLDTTXAHoSoDiemTongService tblQLDTTXAHoSoDiemTongService;
	
	
	 private String hoTen;
	 private Long thongBaoId;
	 
	 private int trangThaiId;
	 
	 private TblQLDTTXAThongBaoTuyenSinh tblQLDTTXAThongBaoTuyenSinh;
	 private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService;
	 private TblQLDTTXADMKhoa tblQLDTTXADMKhoa;
	 private Double diemDat;
	 private Long khoaId;
	 
	 private List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas;
	 
	 public String form() throws Exception 
	 { 
		 Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	     
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	    
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient");
	    
	    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HoSo_DiemXetTuyen", "xem")){ 
	        return "noprivilege"; 
	    } 
	    tblQLDTTXAThongBaoTuyenSinhService = (TblQLDTTXAThongBaoTuyenSinhService) context.getBean("tbl_QLDTTXA_ThongBaoTuyenSinhClient");
	    
	    tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");
		
	    tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhService.findAlltblQLDTTXAThongBaoTuyenSinh(20, 1);

	    tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findAlltblQLDTTXADMKhoa( 0, 0);

	    return Action.SUCCESS;
	 }
	 
  public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
     
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient");
    
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HoSo_DiemXetTuyen", "xem")){ 
        return "noprivilege"; 
    } 
    
    //////////////////get Service
    tblQLDTTXAThongBaoTuyenSinhService = (TblQLDTTXAThongBaoTuyenSinhService) context.getBean("tbl_QLDTTXA_ThongBaoTuyenSinhClient");
 
    tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");
	  
    tblQLDTTXADMThangDiemService = (TblQLDTTXADMThangDiemService) context.getBean("tbl_QLDTTXA_DM_ThangDiemClient");
	  
	tblQLDTTXAHoSoXetTuyenService = (TblQLDTTXAHoSoXetTuyenService) context.getBean("tbl_QLDTTXA_HoSoXetTuyenClient");
	   
	tblQLDTTXAHoSoDiemXetTuyenService = (TblQLDTTXAHoSoDiemXetTuyenService) context.getBean("tbl_QLDTTXA_HoSo_DiemXetTuyenClient");
	
	tblQLDTTXADMKetQuaXetTuyenService =(TblQLDTTXADMKetQuaXetTuyenService) context.getBean("tbl_QLDTTXA_DM_KetQuaXetTuyenClient");
	
	tblQLDTTXAHoSoDiemTongService = (TblQLDTTXAHoSoDiemTongService) context.getBean("tbl_QLDTTXA_HoSo_DiemTongClient");

	TblQLDTTXADMDiemDatService tblQLDTTXADMDiemDatService = (TblQLDTTXADMDiemDatService) context.getBean("tbl_QLDTTXA_DM_DiemDatClient");
;
	//////////////////
    tblQLDTTXADMThangDiems = tblQLDTTXADMThangDiemService.findAlltblQLDTTXADMThangDiem(0, 0);
    
    tblQLDTTXADMKetQuaXetTuyens = tblQLDTTXADMKetQuaXetTuyenService.findAlltblQLDTTXADMKetQuaXetTuyen(0, 0);
    
    //thong bao Tuyen Sinh
      List<ConditionReport> cons = new ArrayList<ConditionReport>(); 

      if(thongBaoId != null && thongBaoId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("id");
		    con.setValue1(thongBaoId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else {
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("thoiGianBatDau");
		    con.setValue1("");
		    con.setOperator("max");
		    cons.add(con);
	  }
      tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhService.findtblQLDTTXAThongBaoTuyenSinhByCondition(cons, 0, 0);
      
      tblQLDTTXAThongBaoTuyenSinh = new TblQLDTTXAThongBaoTuyenSinh();
      for(TblQLDTTXAThongBaoTuyenSinh tbl : tblQLDTTXAThongBaoTuyenSinhs){
    	  tblQLDTTXAThongBaoTuyenSinh = tbl;
      }
      
      tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhService.findAlltblQLDTTXAThongBaoTuyenSinh(20, 1);

      //dm khoa
	  if(khoaId != null && khoaId != 0){
		  tblQLDTTXADMKhoa = tblQLDTTXADMKhoaService.gettblQLDTTXADMKhoa(khoaId);
			 
	  } else {
		    tblQLDTTXADMKhoa = tblQLDTTXADMKhoaService.gettblQLDTTXADMKhoa(new Long(1));
	  }
	  
	  tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findAlltblQLDTTXADMKhoa( 0, 0);

      ////////////get HoSo XetTuyen
      cons.clear();
	   
	  
	  if(thongBaoId != null && thongBaoId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("thongBaoId.id");
		    con.setValue1(thongBaoId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else if(tblQLDTTXAThongBaoTuyenSinh.getId() != null && !tblQLDTTXAThongBaoTuyenSinh.getId().equals(new Long(0))){
		  	ConditionReport con = new ConditionReport(); 
		    con.setCol("thongBaoId.id");
		    con.setValue1(tblQLDTTXAThongBaoTuyenSinh.getId());
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  if(khoaId != null && khoaId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("khoaId.id");
		    con.setValue1(khoaId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else {
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("khoaId.id");
		    con.setValue1(new Long(1));
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  
	  if(true){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("maHoSo");
		    con.setValue1("asc");
		    con.setOperator("order");
		    cons.add(con);
	  }
	  
	  tblQLDTTXAHoSoXetTuyens = tblQLDTTXAHoSoXetTuyenService.findtblQLDTTXAHoSoXetTuyenByCondition(cons, 0, 0); 

	  ////////get Diem Xet Tuyen va Diem Tong
      cons.clear();
	  
	  
	  if(thongBaoId != null && thongBaoId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("hoSoId.thongBaoId.id");
		    con.setValue1(thongBaoId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else if(tblQLDTTXAThongBaoTuyenSinh.getId() != null && !tblQLDTTXAThongBaoTuyenSinh.getId().equals(new Long(0))){
		  	ConditionReport con = new ConditionReport(); 
		    con.setCol("hoSoId.thongBaoId.id");
		    con.setValue1(tblQLDTTXAThongBaoTuyenSinh.getId());
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  
	  if(khoaId != null && khoaId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("hoSoId.khoaId.id");
		    con.setValue1(khoaId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else {
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("hoSoId.khoaId.id");
		    con.setValue1(new Long(1));
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  
	 
	  
	  tblQLDTTXAHoSoDiemXetTuyens = tblQLDTTXAHoSoDiemXetTuyenService.findtblQLDTTXAHoSoDiemXetTuyenByCondition(cons, 0, 0);

	  tblQLDTTXAHoSoDiemTongs = tblQLDTTXAHoSoDiemTongService.findtblQLDTTXAHoSoDiemTongByCondition(cons, 0, 0);

	  cons.clear();
	  if(thongBaoId != null && thongBaoId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("thongBaoId.id");
		    con.setValue1(thongBaoId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else if(tblQLDTTXAThongBaoTuyenSinh.getId() != null && !tblQLDTTXAThongBaoTuyenSinh.getId().equals(new Long(0))){
		  	ConditionReport con = new ConditionReport(); 
		    con.setCol("thongBaoId.id");
		    con.setValue1(tblQLDTTXAThongBaoTuyenSinh.getId());
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  if(khoaId != null && khoaId != 0){
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("khoaId.id");
		    con.setValue1(khoaId);
		    con.setOperator("eq");
		    cons.add(con);
	  } else {
		    ConditionReport con = new ConditionReport(); 
		    con.setCol("khoaId.id");
		    con.setValue1(new Long(1));
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  if(true){
		  ConditionReport con = new ConditionReport(); 
		    con.setCol("suDung");
		    con.setValue1(true);
		    con.setOperator("eq");
		    cons.add(con);
	  }
	  List<TblQLDTTXADMDiemDat> dMDiemDats  = tblQLDTTXADMDiemDatService.findtblQLDTTXADMDiemDatByCondition(cons , 1, 1);
	  for(TblQLDTTXADMDiemDat tbl : dMDiemDats) {
		  diemDat = tbl.getDiemDat();
	  }
	  
	  return Action.SUCCESS; 
	  
      } 
      catch(Exception e){ 
      System.out.println("Loi Report tbl_QLDTTXA_HoSo_DiemXetTuyen Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 
  
public Collection<ViewHoSoXetTuyenDiem> getViewHoSoXetTuyenDiems() {
	return viewHoSoXetTuyenDiems;
}
public void setViewHoSoXetTuyenDiems(
		Collection<ViewHoSoXetTuyenDiem> viewHoSoXetTuyenDiems) {
	this.viewHoSoXetTuyenDiems = viewHoSoXetTuyenDiems;
}
public List<TblQLDTTXAThongBaoTuyenSinh> getTblQLDTTXAThongBaoTuyenSinhs() {
	return tblQLDTTXAThongBaoTuyenSinhs;
}
public void setTblQLDTTXAThongBaoTuyenSinhs(
		List<TblQLDTTXAThongBaoTuyenSinh> tblQLDTTXAThongBaoTuyenSinhs) {
	this.tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhs;
}
public Long getThongBaoId() {
	return thongBaoId;
}
public void setThongBaoId(Long thongBaoId) {
	this.thongBaoId = thongBaoId;
}

public String getHoTen() {
	return hoTen;
}
public void setHoTen(String hoTen) {
	this.hoTen = hoTen;
}
public void setTblQLDTTXAHoSoDiemXetTuyens(
		Collection<TblQLDTTXAHoSoDiemXetTuyen> tblQLDTTXAHoSoDiemXetTuyens) {
	this.tblQLDTTXAHoSoDiemXetTuyens = tblQLDTTXAHoSoDiemXetTuyens;
}
public Collection<TblQLDTTXADMKetQuaXetTuyen> getTblQLDTTXADMKetQuaXetTuyens() {
	return tblQLDTTXADMKetQuaXetTuyens;
}
public void setTblQLDTTXADMKetQuaXetTuyens(
		Collection<TblQLDTTXADMKetQuaXetTuyen> tblQLDTTXADMKetQuaXetTuyens) {
	this.tblQLDTTXADMKetQuaXetTuyens = tblQLDTTXADMKetQuaXetTuyens;
}
public Collection<TblQLDTTXAHoSoDiemTong> getTblQLDTTXAHoSoDiemTongs() {
	return tblQLDTTXAHoSoDiemTongs;
}
public void setTblQLDTTXAHoSoDiemTongs(
		Collection<TblQLDTTXAHoSoDiemTong> tblQLDTTXAHoSoDiemTongs) {
	this.tblQLDTTXAHoSoDiemTongs = tblQLDTTXAHoSoDiemTongs;
}
public TblQLDTTXAThongBaoTuyenSinh getTblQLDTTXAThongBaoTuyenSinh() {
	return tblQLDTTXAThongBaoTuyenSinh;
}
public void setTblQLDTTXAThongBaoTuyenSinh(
		TblQLDTTXAThongBaoTuyenSinh tblQLDTTXAThongBaoTuyenSinh) {
	this.tblQLDTTXAThongBaoTuyenSinh = tblQLDTTXAThongBaoTuyenSinh;
}

public Double getDiemDat() {
	return diemDat;
}
public void setDiemDat(Double diemDat) {
	this.diemDat = diemDat;
}
public Long getKhoaId() {
	return khoaId;
}
public void setKhoaId(Long khoaId) {
	this.khoaId = khoaId;
}
public List<TblQLDTTXADMKhoa> getTblQLDTTXADMKhoas() {
	return tblQLDTTXADMKhoas;
}
public void setTblQLDTTXADMKhoas(List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas) {
	this.tblQLDTTXADMKhoas = tblQLDTTXADMKhoas;
}
public TblQLDTTXADMKhoa getTblQLDTTXADMKhoa() {
	return tblQLDTTXADMKhoa;
}
public void setTblQLDTTXADMKhoa(TblQLDTTXADMKhoa tblQLDTTXADMKhoa) {
	this.tblQLDTTXADMKhoa = tblQLDTTXADMKhoa;
}
public int getTrangThaiId() {
	return trangThaiId;
}
public void setTrangThaiId(int trangThaiId) {
	this.trangThaiId = trangThaiId;
}
}
